package com.hailian.fintech.hlautoserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hailian.fintech.hlautoserver.entity.User;
import com.hailian.fintech.hlautoserver.mapper.UserMapper;
import com.hailian.fintech.hlautoserver.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wk
 * @since 2020-05-20
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService,UserDetailsService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User showinfo(String username) {
        return userMapper.showinfo(username);
    }


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.info("用户进行认证的用户名为：{}",s);
        User tUser = new User();
        tUser.setUsername(s);
        User tUser1 = this.baseMapper.selectOne(new Wrapper<User>() {
            @Override
            public User getEntity() {
                return tUser;
            }

            @Override
            public MergeSegments getExpression() {
                return null;
            }

            @Override
            public String getSqlSegment() {
                return null;
            }
        });
        if (tUser1==null){
            throw  new UsernameNotFoundException("用户名不存在");
        }
        return new org.springframework.security.core.userdetails.User(s,tUser1.getPasswprd(), AuthorityUtils.createAuthorityList("admin"));
    }
}
